﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace Cashier.utils
{
    public class Config
    {
        private static XmlDocument doc = null;
        private static XmlElement root = null;

        /// <summary>
        /// 版本号
        /// </summary>
        public static string VERSION;
        /// <summary>
        /// 库存少于10件发生警告
        /// </summary>
        public static int STOCK_WARING;
        /// <summary>
        /// 打印POS小票抬头名称
        /// </summary>
        public static String SHOP_NAME;
        /// <summary>
        /// 是否允许POS结帐欠款
        /// </summary>
        public static bool IS_POS_DEBT;
        /// <summary>
        /// 权限按钮状态
        /// </summary>
        public static String RIGHTS_BUTTON_STATE;
        /// <summary>
        /// 小票打印预览
        /// </summary>
        public static bool TICKET_PRINT_PREVIEW;
        /// <summary>
        /// POS销售模式
        /// </summary>
        public static String POS_MODEL;
        /// <summary>
        /// 小票打印促销信息
        /// </summary>
        public static String TICKET_PROMOTIONS;
        /// <summary>
        /// 数据库类型
        /// </summary>
        public static String DBTYPE;
        /// <summary>
        /// 用户名
        /// </summary>
        public static String USERNAME;
        /// <summary>
        /// 密码
        /// </summary>
        public static String PASSWORD;

        /// <summary>
        /// 是否使用积分支付
        /// </summary>
        public static bool ISPAY;

        /// <summary>
        /// 金额兑换积分比例
        /// </summary>
        public static int SPENDEXCHANGE;

        /// <summary>
        /// 积分兑换金额比例
        /// </summary>
        public static int EXCHANGEMONEY;

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static Config()
        {
            try
            {
                FileInfo file = new FileInfo(System.Windows.Forms.Application.StartupPath + @"\config.xml");
                if (file.Exists)
                {
                    doc = new XmlDocument();
                    doc.Load(file.FullName);
                    root = doc.DocumentElement;

                    //版本号
                    VERSION = GetNodeInnerText("/config/setting/version");

                    //库存告警
                    String stock = GetNodeInnerText("/config/setting/stock_waring");
                    String reg = @"^\+?[1-9][0-9]*$";
                    if (!String.IsNullOrEmpty(stock) && Regex.IsMatch(stock, reg))
                        STOCK_WARING = Int32.Parse(stock);

                    //POS小票名称
                    SHOP_NAME = GetNodeInnerText("/config/setting/pos_shopname");
                    
                    //是否欠帐
                    String debt = GetNodeInnerText("/config/setting/pos_isdebt");
                    if (debt.Equals("true") || debt.Equals("false"))
                        IS_POS_DEBT = Boolean.Parse(debt);

                    //权限按钮状态
                    RIGHTS_BUTTON_STATE = GetNodeInnerText("/config/setting/rights_button_state");

                    //小票打印预览
                    String ticketPreview = GetNodeInnerText("/config/setting/ticket_print_preview");
                    if (ticketPreview.Equals("true") || debt.Equals("false"))
                        TICKET_PRINT_PREVIEW = Boolean.Parse(ticketPreview);

                    //POS销售模式
                    POS_MODEL = GetNodeInnerText("/config/setting/pos_model");

                    //小票打印促销信息
                    TICKET_PROMOTIONS = GetNodeInnerText("/config/setting/ticket_promotions");

                    //数据库类型
                    DBTYPE = GetNodeInnerText("/config/setting/database/dbtype");

                    //用户名
                    USERNAME = GetNodeInnerText("/config/setting/login/username");

                    //密码
                    PASSWORD = GetNodeInnerText("/config/setting/login/password");

                    //是否支持积分支付
                    String isPay_val = GetNodeInnerText("/config/setting/integral/ispay");
                    if (String.IsNullOrEmpty(isPay_val))
                    {
                        ISPAY = false;
                    }
                    else
                    {
                        ISPAY = Boolean.Parse(isPay_val);
                    }

                    //金额兑换积分比例
                    String spM = GetNodeInnerText("/config/setting/integral/spendexchange");
                    if (!String.IsNullOrEmpty(spM))
                    {
                        SPENDEXCHANGE = Int32.Parse(spM);
                    }

                    //积分兑换金额比例
                    String exM = GetNodeInnerText("/config/setting/integral/exchangemoney");
                    if (!String.IsNullOrEmpty(exM))
                    {
                        EXCHANGEMONEY = Int32.Parse(exM);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取节点值
        /// </summary>
        /// <param name="xPath"></param>
        /// <returns></returns>
        protected static String GetNodeInnerText(String xPath)
        {
            String innerText = String.Empty;
            try
            {
                XmlNode node = root.SelectSingleNode(xPath);
                if (node != null)
                    innerText = node.InnerText;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return innerText;
        }

        /// <summary>
        /// 编辑节点值
        /// </summary>
        /// <param name="xPath"></param>
        /// <param name="value"></param>
        public static void EditNodeInnerText(String xPath, String value)
        {
            try
            {
                XmlNode node = root.SelectSingleNode(xPath);
                if (node != null)
                    node.InnerText = value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 保存XML文档
        /// </summary>
        public static void SaveXml()
        {
            try
            {
                doc.Save(System.Windows.Forms.Application.StartupPath + @"\config.xml");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 设置库存告警数量
        /// </summary>
        /// <param name="value"></param>
        public static void SetStockWaring(String value)
        {
            EditNodeInnerText("/config/setting/stock_waring", value);
        }

        /// <summary>
        /// POS小票抬头名称
        /// </summary>
        /// <param name="value"></param>
        public static void SetShopName(String value)
        {
            EditNodeInnerText("/config/setting/pos_shopname", value);
        }

        /// <summary>
        /// 设置是否可以赊帐
        /// </summary>
        /// <param name="value"></param>
        public static void SetIsPosDebt(String value)
        {
            EditNodeInnerText("/config/setting/pos_isdebt", value);
        }

        /// <summary>
        /// 设置权限按钮状态
        /// </summary>
        /// <param name="value"></param>
        public static void SetRightsButtonState(String value)
        {
            EditNodeInnerText("/config/setting/rights_button_state", value);
        }

        /// <summary>
        /// 设置小票打印预览
        /// </summary>
        /// <param name="value"></param>
        public static void SetTicketPrintPreview(String value)
        {
            EditNodeInnerText("/config/setting/ticket_print_preview", value);
        }

        /// <summary>
        /// 设置POS销售模式
        /// </summary>
        /// <param name="value"></param>
        public static void SetPosModel(String value)
        {
            EditNodeInnerText("/config/setting/pos_model", value);
        }

        /// <summary>
        /// 设置小票打印促销信息
        /// </summary>
        /// <param name="value"></param>
        public static void SetTicketPromotions(String value)
        {
            EditNodeInnerText("/config/setting/ticket_promotions", value);
        }

        /// <summary>
        /// 设置数据库
        /// </summary>
        /// <param name="value"></param>
        public static void SetDbtype(String value)
        {
            EditNodeInnerText("/config/setting/database/dbtype", value);
        }

        /// <summary>
        /// 设置用户名
        /// </summary>
        /// <param name="value"></param>
        public static void SetUsername(String value)
        {
            EditNodeInnerText("/config/setting/login/username", value);
        }

        /// <summary>
        /// 设置密码
        /// </summary>
        /// <param name="value"></param>
        public static void SetPassword(String value)
        {
            EditNodeInnerText("/config/setting/login/password", value);
        }
    }
}
